package com.migu.contacts.main.my;

import static com.migu.contacts.net.Service.API;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.frank.libandroid.util.Bus;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.MainActivity;
import com.migu.contacts.R;
import com.migu.contacts.bean.DocumentScoreBean;
import com.migu.contacts.bean.UserScoreBean;
import com.migu.contacts.databinding.ActivityScoreBinding;
import com.migu.contacts.databinding.ItemScoreAddBinding;
import com.migu.contacts.databinding.ItemScoreLoginBinding;
import com.migu.contacts.databinding.ItemScoreProductBinding;
import com.migu.contacts.databinding.PopScoreBuyBinding;
import com.migu.contacts.databinding.PopScoreDescBinding;
import com.migu.contacts.net.ResultCallback;

import java.util.List;
import java.util.function.Predicate;

public class ScoreActivity extends BaseActivity<ActivityScoreBinding> {

    @Override
    protected void initData() {
        super.initData();
        API.getUserScore().watch(this, new HttpCallback<UserScoreBean>() {
            @Override
            protected void onSuccess(UserScoreBean userScoreBean) {
                if (userScoreBean != null) {
                    mUserScoreBean = userScoreBean;
                    mBinding.setBean(userScoreBean);
                    List<UserScoreBean.LoginStatus> loginStatus = userScoreBean.getLoginStatus();
                    loginAdapter.setData(loginStatus);
                    List<UserScoreBean.ScoreSettingList> scoreSettingList = userScoreBean.scoreSettingList;
                    scoreSettingList.removeIf(new Predicate<UserScoreBean.ScoreSettingList>() {
                        @Override
                        public boolean test(UserScoreBean.ScoreSettingList scoreSettingList) {
                            return scoreSettingList.type == 3;
                        }
                    });
                    scoreAdapter.setData(scoreSettingList);
                    productScoreListBaseAdapter.setData(userScoreBean.productScoreList);
                }
            }
        });
    }

    UserScoreBean mUserScoreBean = null;

    @BindAdapter(R.id.loginRecyclerView)
    BaseAdapter<ItemScoreLoginBinding, UserScoreBean.LoginStatus> loginAdapter = new BaseAdapter<ItemScoreLoginBinding, UserScoreBean.LoginStatus>() {
    };

    @BindAdapter(R.id.scoreRecyclerView)
    BaseAdapter<ItemScoreAddBinding, UserScoreBean.ScoreSettingList> scoreAdapter = new BaseAdapter<ItemScoreAddBinding, UserScoreBean.ScoreSettingList>() {
        @OnClick(R.id.go)
        public void go(UserScoreBean.ScoreSettingList bean) {
            if (bean.type == 2) {
                finish();
                Bus.emit(MainActivity.class.getName());
            } else {
                start(InviteActivity.class);
            }
        }
    };
    @BindAdapter(R.id.productRecyclerView)
    BaseAdapter<ItemScoreProductBinding, UserScoreBean.ProductScoreList> productScoreListBaseAdapter = new BaseAdapter<ItemScoreProductBinding, UserScoreBean.ProductScoreList>() {
        @OnClick(R.id.buy)
        public void buy(UserScoreBean.ProductScoreList productScoreList) {
            showBuyPop(productScoreList);
        }
    };

    private void showBuyPop(UserScoreBean.ProductScoreList productScoreList) {
        CenterPopup bottomPopup = new CenterPopup<PopScoreBuyBinding>(mActivity) {

            @Override
            protected void initData() {
                mBinding.setBean(productScoreList);
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                API.scoreOrderCreate(productScoreList.id).watch(mActivity, new HttpCallback<Double>() {
                    @Override
                    protected void onSuccess(Double aBoolean) {
                        if (aBoolean != null && mUserScoreBean != null) {
                            ToastUtils.showShort("兑换成功");
                            mUserScoreBean.score = aBoolean;
                            ScoreActivity.this.mBinding.setBean(mUserScoreBean);
                        }
                        dismiss();
                    }
                });
            }

            @OnClick(R.id.quit)
            public void quit() {
                dismiss();
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    @Override
    protected void initView() {
        super.initView();
        LinearLayoutManager layoutManager = (LinearLayoutManager) mBinding.loginRecyclerView.getLayoutManager();
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    }

    public void showDesc(View view) {
        CenterPopup bottomPopup = new CenterPopup<PopScoreDescBinding>(mActivity) {

            @OnClick(R.id.confirm)
            public void confirm() {
                dismiss();
            }


            @Override
            protected void initData() {
                super.initData();
                API.getDocumentScore().enqueue(new ResultCallback<DocumentScoreBean>() {
                    @Override
                    protected void onSuccess(DocumentScoreBean objects) {
                        mBinding.setBean(objects);
                    }
                });
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }
}